# Concave
# Copyright (C) 2012 Daniel Malmqvist
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
import constants
import helperfunctions
import g
import random
import os

from pygame.locals import RLEACCEL
import magic
from item import Item

class Player():
    def __init__(self,startX,startY, mapa, name):
        self.name = name
        self.deepestLevel = 0
        self.money = 100
        self.magicWait = self.magicWaitStart = 50
        self.buffs = []
        self.dots = []
        
        self.killedBosses = dict()
        
        
        try:
            f = open(self.name + ".sav", 'r')
        except IOError:
            f = None

        if f:
            print "save file exists"
            
            self.exp = int(f.readline())
            self.deepestLevel = int(f.readline())
            self.money = int(f.readline())
            
            self.level = 1
            self.expToPrevLevel = 0
            self.expToNextLevel = 10
            
            for i in range(0, len(g.expLevels())):
                if self.exp > g.expLevels()[i]:
                    self.level = i + 2
                    self.expToPrevLevel = g.expLevels()[i]
                    self.expToNextLevel = g.expLevels()[i+1] 
            
            
            
            
            self.str = 35 + (self.level * 2)
            self.mag = 5 + (self.level * 1)
            self.vit = 50 + (self.level * 2)
            
            
            
            
            
            self.type = constants.DAGGER()
            self.headlevel = 0
            self.bodylevel = 1
            self.legslevel = 1
            self.feetlevel = 1
            
            belt = f.readline().split("\n")
            belt = belt[0].split(",")
            
            if belt[0] == "0":
                self.beltlevel = 0
            else:
                self.beltlevel = int(belt[1])
                g.getInventroy().beltArmor = Item(-1, -1, int(belt[0]), int(belt[1]))
                g.getInventroy().beltArmor.strExtra = int(belt[2])
                g.getInventroy().beltArmor.magExtra = int(belt[3])
                g.getInventroy().beltArmor.vitExtra = int(belt[4])
                g.getInventroy().beltArmor.generateName()
                #print "belt", g.getInventroy().beltArmor.getText()
                
            body = f.readline().split("\n")[0].split(",")
            if body[0] == "0":
                self.bodylevel = 0
            else:
                self.bodylevel = int(body[1])
                g.getInventroy().bodyArmor = Item(-1, -1, int(body[0]), int(body[1]))
                g.getInventroy().bodyArmor.strExtra = int(body[2])
                g.getInventroy().bodyArmor.magExtra = int(body[3])
                g.getInventroy().bodyArmor.vitExtra = int(body[4])
                g.getInventroy().bodyArmor.generateName()
                #print "body", g.getInventroy().bodyArmor.getText()
            
            
            
            feet = f.readline().split("\n")[0].split(",")
            if feet[0] == "0":
                self.feetlevel = 0
            else:
                self.feetlevel = int(feet[1])
                g.getInventroy().feetArmor = Item(-1, -1, int(feet[0]), int(feet[1]))
                g.getInventroy().feetArmor.strExtra = int(feet[2])
                g.getInventroy().feetArmor.magExtra = int(feet[3])
                g.getInventroy().feetArmor.vitExtra = int(feet[4])
                g.getInventroy().feetArmor.generateName()
                #print "feet", g.getInventroy().feetArmor.getText()
            
            
            head = f.readline().split("\n")[0].split(",")
            if head[0] == "0":
                self.headlevel = 0
            else:
                self.headlevel = int(feet[1])
                g.getInventroy().headArmor = Item(-1, -1, int(head[0]), int(head[1]))
                g.getInventroy().headArmor.strExtra = int(head[2])
                g.getInventroy().headArmor.magExtra = int(head[3])
                g.getInventroy().headArmor.vitExtra = int(head[4])
                g.getInventroy().headArmor.generateName()
                #print "head", g.getInventroy().headArmor.getText()
            
            
            legs = f.readline().split("\n")[0].split(",")
            if legs[0] == "0":
                self.legslevel = 0
            else:
                self.legslevel = int(legs[1])
                g.getInventroy().legsArmor = Item(-1, -1, int(legs[0]), int(legs[1]))
                g.getInventroy().legsArmor.strExtra = int(legs[2])
                g.getInventroy().legsArmor.magExtra = int(legs[3])
                g.getInventroy().legsArmor.vitExtra = int(legs[4])
                g.getInventroy().legsArmor.generateName()
                #print "legs", g.getInventroy().legsArmor.getText()
                
            weapon = f.readline().split("\n")[0].split(",")
            if weapon[0] == "0":
                print "error no weapon"
            else:
                self.type = int(weapon[0])
                g.getInventroy().weapon = Item(-1, -1, int(weapon[0]), int(weapon[1]))
                g.getInventroy().weapon.strExtra = int(weapon[2])
                g.getInventroy().weapon.magExtra = int(weapon[3])
                g.getInventroy().weapon.vitExtra = int(weapon[4])
                g.getInventroy().weapon.generateName()
                #print "weapon", g.getInventroy().weapon.getText()
            for x in range(0,5):
                for y in range(0,5):
                    itemA = f.readline().split("\n")[0].split(",")
                    
                    if itemA[0] != "0":
                        item = Item(-1, -1, int(itemA[0]), int(itemA[1]))
                        item.strExtra = int(itemA[2])
                        item.magExtra = int(itemA[3])
                        item.vitExtra = int(itemA[4])
                        item.generateName()
                        g.getInventroy().addItem(item)
                    
            
        else:
            print "save file does not exist"
        
            self.level = 1
            self.exp = 0
            self.expToPrevLevel = 0
            self.expToNextLevel = 10
            
            self.str = 35 + (self.level * 2)
            self.mag = 5 + (self.level * 1)
            self.vit = 50 + (self.level * 2)
            
            self.maxHp = self.hp = (self.vit*10)
            self.maxMp = self.mp = (self.mag*10)
            
            
            self.type = constants.DAGGER()
            self.headlevel = 0
            self.bodylevel = 1
            self.legslevel = 1
            self.feetlevel = 1
            self.beltlevel = 0
        
        
        self.maxHp = self.hp = (self.vit*10)
        self.maxMp = self.mp = (self.mag*10)
        
        self.hitStartX = 16
        self.hitStopX = 48
        self.hitStartY = 48
        self.hitStopY = 64
        
        self.x = startX-self.hitStartX
        self.y = startY-self.hitStartY
        
        self.mapLevel = g.getCurrentLevel()
        
        self.map = mapa
        
        self.walksheet = []
        self.armorsheet = []
        self.leggssheet = []
        
        
        self.walkNorth = []
        self.walkSouth = []
        self.walkWest = []
        self.walkEast = []
        
        self.attackNorth = []
        self.attackSouth = []
        self.attackWest = []
        self.attackEast = []
        
        self.magicNorth = []
        self.magicSouth = []
        self.magicWest = []
        self.magicEast = []
        
        self.inventoryImage = 0
        self.hurt = []
        
        self.loadImages()

        
        self.attacking = False
        self.castingMagic = False
        
        
        self.direction = constants.SOUTH()
        
        self.imageIndex = 0
        self.imageWait = 10
        self.imageAttackWait = 5
        self.imageCurrentWait = 0
        
        self.moving = True
        

        self.magicSelected = constants.HEALMAGIC()
        
        self.hitLeft = 12
        self.hitRight = 12
        self.hitUp = 12
        self.hitDown = 16
        
        self.isOnAttackUpdate = False
        self.isOnMagicUpdate = False
        self.updateIndex = 0
        self.armor = 0
        #self.updateFromInventory()
    
    def save(self):
        """
        self.level = 1
        self.exp = 0
        self.expToPrevLevel = 0
        self.expToNextLevel = 10
        """
        
        f = open(self.name + ".sav", 'w')
        text = str(self.exp) + "\n" 
        text += str(self.deepestLevel) + "\n"
        text += str(self.money) + "\n"
        text += str(g.getInventroy().toSaveText())
        f.write(text)
        f.flush()
        f.close()
        
    def update(self, enemiesInRange = []):
        self.updateIndex += 1
        
        for buff in self.buffs:
            if not buff.update():
                self.buffs.remove(buff)
        
        
        if self.castingMagic:
            self.magicWait -= 1
            
            if self.magicWait < 0:
                for m in g.getMagic():
                    if m.caster == self:
                        return False
            
                g.addMagic(magic.Magic(self.magicSelected, self, enemiesInRange))
                
                self.castingMagic = False
        
        if (not self.attacking and self.hp > 0 and self.updateIndex%5 == 0):
            self.hp = min(max(self.hp+1, self.hp + self.maxHp/300), self.maxHp)
            self.mp = min(max(self.mp+1, self.mp + self.maxMp/300), self.maxMp)
            
        
        if (self.attacking and self.updateIndex%30 == 0):
            attackBox = self.getAttackRect()
            hits = []
            for e in enemiesInRange:
                if attackBox.colliderect(e.getHitRect()):
                    hits.append(e)
            for e in hits:
                e.takeDamage(self.getDmg(), self)
            
            if len(hits) > 0:
                g.getSoundSystem().playSound(0)
            else:
                g.getSoundSystem().playSound(1)
        
        if (self.castingMagic and self.isOnMagicUpdate):
            for m in g.getMagic():
                if m.caster == self:
                    return False
            
            g.addMagic(magic.Magic(self.magicSelected, self, enemiesInRange))
        
                
    def getName(self):
        return self.name + ", level:" + str(self.level)
    def getDmg(self):
        return self.level * self.str * float(random.randint(0, 200))/100
    
    def getExp(self, expToGain):
        self.exp += expToGain
        self.money += expToGain
        while self.exp >= self.expToNextLevel:
            self.gainLevel()
        
        
    def gainLevel(self):
        self.expToPrevLevel = self.expToNextLevel
        
        self.str += 2
        self.mag += 1
        self.vit += 2
        self.maxHp = self.hp = (self.vit*10)
        self.level += 1
        
        
        self.expToNextLevel = g.expLevels()[self.level-1]
        print "current level", self.level
        
    def attack(self):
        if not self.attacking:
            self.imageIndex = 0
            self.imageCurrentWait = 0
            self.attacking = True
            self.castingMagic = False
            self.moving = False
        if self.imageIndex == 2 and self.imageCurrentWait == 0 and self.attacking:
            self.isOnAttackUpdate = True
        else:
            self.isOnAttackUpdate = False

    def beginCastMagic(self, magicId):
        magic = g.getMagics()[magicId]
        
        
        self.magicSelected = magicId
        self.imageIndex = 0
        self.imageCurrentWait = 0
        self.castingMagic = True
        self.magicWait = self.magicWaitStart = magic[5]
        self.attacking = False
        self.moving = False

    def magic(self):
        if not self.castingMagic:
            self.imageIndex = 0
            self.imageCurrentWait = 0
            self.castingMagic = True
            self.attacking = False
            self.moving = False
        if self.imageIndex == 4 and self.castingMagic:
            self.isOnMagicUpdate = True
        else:
            self.isOnMagicUpdate = False      
        
    def getCurrentImage(self):
        if self.attacking:
            self.imageCurrentWait += 1
            if self.imageCurrentWait == self.imageAttackWait:
                self.imageCurrentWait = 0
                self.imageIndex += 1

            if self.imageIndex == len(self.attackNorth):
                self.imageIndex = 0
            
            
            if self.direction == constants.NORTH():
                return self.attackNorth[self.imageIndex]
            elif self.direction == constants.EAST():
                return self.attackEast[self.imageIndex]
            elif self.direction == constants.SOUTH():
                return self.attackSouth[self.imageIndex]
            else:
                return self.attackWest[self.imageIndex]    
                
        elif self.castingMagic:
            magicImageId = (self.magicWaitStart-self.magicWait)/(self.magicWaitStart/7)
            magicImageId = min(magicImageId, 6)

            if self.direction == constants.NORTH():
                return self.magicNorth[magicImageId]
            elif self.direction == constants.EAST():
                return self.magicEast[magicImageId]
            elif self.direction == constants.SOUTH():
                return self.magicSouth[magicImageId]
            else:
                return self.magicWest[magicImageId]
            
        elif self.moving:
            self.imageIndex += 1
            
            self.imageIndex = self.imageIndex%((len(self.walkNorth) - 1)*10)
            if self.imageIndex == 0:
                self.imageIndex = 1
            
            if self.direction == constants.NORTH():
                return self.walkNorth[self.imageIndex/10]
            elif self.direction == constants.EAST():
                return self.walkEast[self.imageIndex/10]
            elif self.direction == constants.SOUTH():
                return self.walkSouth[self.imageIndex/10]
            elif self.direction == constants.WEST():
                return self.walkWest[self.imageIndex/10]
            self.moving = False
        else:
            if self.direction == constants.NORTH():
                return self.walkNorth[0]
            elif self.direction == constants.EAST():
                return self.walkEast[0]
            elif self.direction == constants.SOUTH():
                return self.walkSouth[0]
            elif self.direction == constants.WEST():
                return self.walkWest[0]
    
    
    def loadImage(self,indexX, indexY):
        rect = pygame.Rect((indexX*64,indexY*64, 64, 64))
                
        image = pygame.Surface(rect.size, pygame.SRCALPHA)
        
        image.blit(self.walksheet, (0, 0), rect)
        image.set_colorkey(-1, RLEACCEL)
        return image
    
    def moveTo(self, directions, enemiesInRange):
        if self.attacking:
            stepLength = 1
        else:
            stepLength = 4
        oldx = self.x
        oldy = self.y
        
        if len(directions) <= 0 or len(directions) >= 4:
            self.moving = False
            return False
        
        if len(directions) == 3:
            if constants.EAST() in directions and constants.WEST() in directions:
                if constants.NORTH() in directions:
                    directions = [constants.NORTH()]
                else:
                    directions = [constants.SOUTH()]
            elif constants.NORTH() in directions and constants.SOUTH() in directions:
                if constants.WEST() in directions:
                    directions = [constants.WEST()]
                else:
                    directions = [constants.EAST()]
        
        if len(directions) == 2:
            if ((constants.EAST() in directions and constants.WEST() in directions) 
                or (constants.NORTH() in directions and constants.SOUTH() in directions)):
                return False
            
            self.moving = True
            if not self.attacking:
                self.direction = directions[0]
            
            if constants.EAST() in directions and constants.NORTH() in directions:
                self.y -= stepLength
                self.x += stepLength
                if self.canMoveTo(enemiesInRange):
                    return True
                
                self.y = oldy
                self.x = oldx + stepLength
                
                if self.canMoveTo(enemiesInRange):
                    if not self.attacking:
                        self.direction = constants.EAST()
                    return True
                
                self.y = oldy - stepLength
                self.x = oldx
                
                if self.canMoveTo(enemiesInRange):
                    if not self.attacking:
                        self.direction = constants.NORTH()
                    return True
                
                self.y = oldy
                self.x = oldx
                return False
            elif constants.NORTH() in directions and constants.WEST() in directions:
                self.y -= stepLength
                self.x -= stepLength
                if self.canMoveTo(enemiesInRange):
                    return True
                
                self.y = oldy
                self.x = oldx - stepLength
                
                if self.canMoveTo(enemiesInRange):
                    if not self.attacking:
                        self.direction = constants.WEST()
                    return True
                
                self.y = oldy - stepLength
                self.x = oldx
                
                if self.canMoveTo(enemiesInRange):
                    if not self.attacking:
                        self.direction = constants.NORTH()
                    return True
                
                self.y = oldy
                self.x = oldx
                return False
            elif constants.SOUTH() in directions and constants.WEST() in directions:
                self.y += stepLength
                self.x -= stepLength
                if self.canMoveTo(enemiesInRange):
                    return True
                
                self.y = oldy
                self.x = oldx - stepLength
                
                if self.canMoveTo(enemiesInRange):
                    if not self.attacking:
                        self.direction = constants.WEST()
                    return True
                
                self.y = oldy + stepLength
                self.x = oldx
                
                if self.canMoveTo(enemiesInRange):
                    if not self.attacking:
                        self.direction = constants.SOUTH()
                    return True
                
                self.y = oldy
                self.x = oldx
                return False
            elif constants.SOUTH() in directions and constants.EAST() in directions:
                self.y += stepLength
                self.x += stepLength
                if self.canMoveTo(enemiesInRange):
                    return True
                
                self.y = oldy
                self.x = oldx + stepLength
                
                if self.canMoveTo(enemiesInRange):
                    if not self.attacking:
                        self.direction = constants.EAST()
                    return True
                
                self.y = oldy + stepLength
                self.x = oldx
                
                if self.canMoveTo(enemiesInRange):
                    if not self.attacking:
                        self.direction = constants.SOUTH()
                    return True
                
                self.y = oldy
                self.x = oldx
                return False
            
        elif len(directions) == 1:
            self.moving = True
            if not self.attacking:
                self.direction = directions[0]
            
            if (directions[0] == constants.NORTH()):
                self.y -= stepLength
                if self.canMoveTo(enemiesInRange):
                    return True
                else:
                    self.y += stepLength
                    return False
                
            elif (directions[0] == constants.SOUTH()):
                self.y += stepLength
                if self.canMoveTo(enemiesInRange):
                    return True
                else:
                    self.y -= stepLength
                    return False
            elif (directions[0] == constants.WEST()):
                self.x -= stepLength
                if self.canMoveTo(enemiesInRange):
                    return True
                else:
                    self.x += stepLength
                    return False
            elif (directions[0] == constants.EAST()):
                self.x += stepLength
                if self.canMoveTo(enemiesInRange):
                    return True
                else:
                    self.x -= stepLength
                    return False
                
        self.moving = False
        return False
    def getHitRect(self):
        return pygame.Rect(self.x+self.hitStartX-48, self.y+self.hitStartY-48, self.hitStopX-self.hitStartX, self.hitStopY-self.hitStartY)
    def getItemHitRect(self):
        return pygame.Rect(self.x+self.hitStartX-48-16, self.y+self.hitStartY-48-16, self.hitStopX-self.hitStartX+32, self.hitStopY-self.hitStartY+32)
    
    def getAttackRect(self):
        # Short weapons
        if (self.type == constants.DAGGER() or self.type == constants.SPEAR() or self.type == constants.STAFF()):
            if self.direction == constants.NORTH():
                box = self.getHitRect().move(0,-16)
            elif self.direction == constants.SOUTH():
                box = self.getHitRect().move(0,16)
            elif self.direction == constants.WEST():
                box = self.getHitRect().move(-32,0)
            elif self.direction == constants.EAST():
                box = self.getHitRect().move(32,0)
                
            box = box.inflate(8,8)
        else:
            if self.direction == constants.NORTH():
                box = self.getHitRect().move(0,-32)
                box = box.inflate(32,32)
            elif self.direction == constants.SOUTH():
                box = self.getHitRect().move(0,32)
                box = box.inflate(32,32)
                
            elif self.direction == constants.WEST():
                box = self.getHitRect().move(-48,0)
                box = box.inflate(8,16)
            elif self.direction == constants.EAST():
                box = self.getHitRect().move(48,0)
                box = box.inflate(8,16)
        return box
    
    def takeDamage(self, hp):
        self.hp -= hp / (self.armor/2) 
        if self.hp <= 0:
            self.hp = 0
            #self.die()
    def canMoveTo(self, enemiesInRange):
        hitRect = self.getHitRect()
        for e in enemiesInRange:
            if hitRect.colliderect(e.getHitRect()):
                return False

        if g.getCurrentLevel() == 0:
            px = self.x + 8
        else:
            px = self.x + 16
        py = self.y
        
        px -= self.hitLeft
        py -= self.hitUp
    
        if self.map[py/32][px/32] != 1:
            return False
        
        return True
    
    def getHpLineWidth(self):
        lineWidth = 58
        lineWidth = float(lineWidth)
        lineWidth *= (float(self.hp)/float(self.maxHp))
        
        lineWidth = int(lineWidth)
        if lineWidth < 0:
            lineWidth = 0
        
        return lineWidth
    def getMpLineWidth(self):
        lineWidth = 58
        lineWidth = float(lineWidth)
        lineWidth *= (float(self.mp)/float(self.maxMp))
        
        lineWidth = int(lineWidth)
        if lineWidth < 0:
            lineWidth = 0
        
        return lineWidth
    def getExpLineWidth(self):
        lineWidth = 58
        lineWidth = float(lineWidth)
        if self.level == 1:
            lineWidth *= (float(self.exp)/float(self.expToNextLevel))
        else:
            e = self.exp - self.expToPrevLevel
            eNext = self.expToNextLevel - self.expToPrevLevel
            lineWidth *= (float(e)/float(eNext))
        
        
        lineWidth = int(lineWidth)
        if lineWidth < 0:
            lineWidth = 0
        
        return lineWidth    
    
    
    
    def loadImages(self):
        self.gridX = 64
        self.gridY = 64
        
        walkpath = os.path.join("graphic","walkcycle")
        
        if self.type in [constants.DAGGER(),constants.LONGSWORD(), constants.RAPIER()]:
            attackpath = os.path.join("graphic","slash")
        else:
            attackpath = os.path.join("graphic","thrust")
            
        hurtPath = os.path.join("graphic","hurt")
        
        walksheet = self.generateImage(walkpath)
        
        attackSheet = self.generateImage(attackpath) 
        
        hurtSheet = self.generateImage(hurtPath)
        
        magicSheet = self.generateImage(os.path.join("graphic","spellcast"))
        
        
        self.walksheet = []
        self.armorsheet = []
        self.leggssheet = []
        
        
        self.walkNorth = []
        self.walkSouth = []
        self.walkWest = []
        self.walkEast = []
        
        self.attackNorth = []
        self.attackSouth = []
        self.attackWest = []
        self.attackEast = []
        
        self.magicNorth = []
        self.magicSouth = []
        self.magicWest = []
        self.magicEast = []
        
        
        
        
        for i in range(1, walksheet.get_width()/self.gridX):
            self.walkNorth.append(helperfunctions.loadImage(walksheet, i, 0, self.gridX, self.gridY))
            self.walkWest.append(helperfunctions.loadImage(walksheet, i, 1, self.gridX, self.gridY))
            self.walkSouth.append(helperfunctions.loadImage(walksheet, i, 2, self.gridX, self.gridY))
            self.walkEast.append(helperfunctions.loadImage(walksheet, i, 3, self.gridX, self.gridY))
        self.standSouth = helperfunctions.loadImage(walksheet, 0, 0, self.gridX, self.gridY)
        self.standWest = helperfunctions.loadImage(walksheet, 0, 1, self.gridX, self.gridY)
        self.standEast = helperfunctions.loadImage(walksheet, 0, 2, self.gridX, self.gridY)
        self.standNorth = helperfunctions.loadImage(walksheet, 0, 3, self.gridX, self.gridY)

        for i in range(0, attackSheet.get_width()/self.gridX):
            self.attackNorth.append(helperfunctions.loadImage(attackSheet, i, 0, self.gridX, self.gridY))
            self.attackWest.append(helperfunctions.loadImage(attackSheet, i, 1, self.gridX, self.gridY))
            self.attackSouth.append(helperfunctions.loadImage(attackSheet, i, 2, self.gridX, self.gridY))
            self.attackEast.append(helperfunctions.loadImage(attackSheet, i, 3, self.gridX, self.gridY))

        for i in range(0, hurtSheet.get_width()/self.gridX):
            self.hurt.append(helperfunctions.loadImage(hurtSheet, i, 0, self.gridX, self.gridY))



        for i in range(0, magicSheet.get_width()/self.gridX):
            self.magicNorth.append(helperfunctions.loadImage(magicSheet, i, 0, self.gridX, self.gridY))
            self.magicWest.append(helperfunctions.loadImage(magicSheet, i, 1, self.gridX, self.gridY))
            self.magicSouth.append(helperfunctions.loadImage(magicSheet, i, 2, self.gridX, self.gridY))
            self.magicEast.append(helperfunctions.loadImage(magicSheet, i, 3, self.gridX, self.gridY))

        
        
        attackSheet = 0
        walksheet = 0
        hurtSheet = 0
        
        self.inventoryImage = pygame.transform.scale2x(self.standEast)
        
    def generateImage(self, path):
        sheet = 0

        sheet = pygame.image.load(os.path.join(path, "BODY_male.png"))
        
        if self.bodylevel == 1:
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_leather_armor_shirt_white.png")), (0,0))
        elif self.bodylevel == 2:
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_robe_shirt_brown.png")), (0,0))
        elif self.bodylevel == 3:
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_leather_armor_torso.png")), (0,0))
        elif self.bodylevel == 4:
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_leather_armor_torso.png")), (0,0))
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_leather_armor_shoulders.png")), (0,0))
        elif self.bodylevel == 5:
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_chain_armor_jacket_purple.png")), (0,0))
        elif self.bodylevel == 6:
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_chain_armor_torso.png")), (0,0))
        elif self.bodylevel == 7:
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_plate_armor_torso.png")), (0,0))
        elif self.bodylevel == 8:
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_plate_armor_torso.png")), (0,0))
            sheet.blit(pygame.image.load(os.path.join(path, "TORSO_plate_armor_arms_shoulders.png")), (0,0))
        
        
        if self.legslevel == 1:
            sheet.blit(pygame.image.load(os.path.join(path, "LEGS_pants_greenish.png")), (0,0))
        elif self.legslevel == 2:
            sheet.blit(pygame.image.load(os.path.join(path, "LEGS_robe_skirt.png")), (0,0))
        elif self.legslevel == 3:
            sheet.blit(pygame.image.load(os.path.join(path, "LEGS_plate_armor_pants.png")), (0,0))
        
        if self.beltlevel == 1:
            sheet.blit(pygame.image.load(os.path.join(path, "BELT_rope.png")), (0,0))
        elif self.beltlevel == 2:
            sheet.blit(pygame.image.load(os.path.join(path, "BELT_leather.png")), (0,0))
        
        
        if self.feetlevel == 1:
            sheet.blit(pygame.image.load(os.path.join(path, "FEET_shoes_brown.png")), (0,0))
        elif self.feetlevel == 2:
            sheet.blit(pygame.image.load(os.path.join(path, "FEET_plate_armor_shoes.png")), (0,0))
        
        if self.headlevel == 1:
            sheet.blit(pygame.image.load(os.path.join(path, "HEAD_robe_hood.png")), (0,0))
        elif self.headlevel == 2:
            sheet.blit(pygame.image.load(os.path.join(path, "HEAD_leather_armor_hat.png")), (0,0))
        elif self.headlevel == 3:
            sheet.blit(pygame.image.load(os.path.join(path, "HEAD_chain_armor_helmet.png")), (0,0))
        elif self.headlevel == 4:
            sheet.blit(pygame.image.load(os.path.join(path, "HEAD_chain_armor_hood.png")), (0,0))
        elif self.headlevel == 5:
            sheet.blit(pygame.image.load(os.path.join(path, "HEAD_plate_armor_helmet.png")), (0,0))
        
        
        return sheet
    
    
    
    
    
    def updateFromInventory(self):
        # Called when player changes gear
        inventory = g.getInventroy()
        
        
        """
        Player Images
        """
        if inventory.beltArmor != 0:
            self.beltlevel = inventory.beltArmor.level
        if inventory.headArmor != 0:
            self.headlevel = inventory.headArmor.level
        if inventory.bodyArmor != 0:
            self.bodylevel = inventory.bodyArmor.level
        if inventory.feetArmor != 0:
            self.feetlevel = inventory.feetArmor.level
        if inventory.legsArmor != 0:
            self.legslevel = inventory.legsArmor.level
            
        self.loadImages()
        
        """
        Player stats
        """
        self.armor = self.beltlevel + self.headlevel + self.bodylevel + self.feetlevel + self.legslevel
        
        
        self.str = 15 + (self.level * 2)
        self.mag = 5 + (self.level * 1)
        self.vit = 10 + (self.level * 2)
        
        
        if inventory.beltArmor != 0:
            self.str += inventory.beltArmor.strExtra
            self.vit += inventory.beltArmor.vitExtra
            self.mag += inventory.beltArmor.magExtra
        if inventory.headArmor != 0:
            self.str += inventory.headArmor.strExtra
            self.vit += inventory.headArmor.vitExtra
            self.mag += inventory.headArmor.magExtra
        if inventory.bodyArmor != 0:
            self.str += inventory.bodyArmor.strExtra
            self.vit += inventory.bodyArmor.vitExtra
            self.mag += inventory.bodyArmor.magExtra
        if inventory.feetArmor != 0:
            self.str += inventory.feetArmor.strExtra
            self.vit += inventory.feetArmor.vitExtra
            self.mag += inventory.feetArmor.magExtra
        if inventory.legsArmor != 0:
            self.str += inventory.legsArmor.strExtra
            self.vit += inventory.legsArmor.vitExtra
            self.mag += inventory.legsArmor.magExtra
        if inventory.weapon != 0:
            self.str += inventory.weapon.strExtra
            self.vit += inventory.weapon.vitExtra
            self.mag += inventory.weapon.magExtra
        
        
        
        self.maxHp = (self.vit*10)
        self.maxMp = (self.mag*10)
        
        self.hp = min(self.hp, self.maxHp)
        self.mp = min(self.mp, self.maxMp)
        
        
        
        
    def getStatsText(self):
        statsText = "STR: " + str(self.str) + "\n"
        statsText += "MAG: " + str(self.mag) + "\n"
        statsText += "VIT: " + str(self.vit) + "\n"
        statsText += "EXP: " + str(self.exp) + "(" + str(self.expToNextLevel) + ")\n"
        statsText += "Armor: " + str(self.armor) + "\n"
        statsText += "Money: " + str(self.money)
        
        return statsText
    
    
    
    
    
    
    